warning('off','MATLAB:singularMatrix')
k=getenv('SLURM_ARRAY_TASK_ID');
kk=str2num(k(1));
version=str2num(k(2));

addpath /scratch/akalandr/SUPPLEMENTAL/CIRC
addpath /scratch/akalandr/SUPPLEMENTAL/Combined
addpath /scratch/akalandr/SUPPLEMENTAL/Inference
addpath /scratch/akalandr/SUPPLEMENTAL/TwoStepMatlab

pc=parcluster('local')
JOB_ID=getenv('SLURM_JOBID')
pc.JobStorageLocation=strcat('/local_scratch/',JOB_ID)
parpool(pc,24)
cd /scratch/akalandr/SUPPLEMENTAL/CIRC
load('GERN.mat')
cd /scratch/akalandr/SUPPLEMENTAL/RESULTS
years = unique(cellfun(@(x) x(2:end),fieldnames(GERN),'UniformOutput', false));

Dname = strcat('M',years{kk});
D = GERN.(Dname);

Pname = strcat('P',years{kk});
P = GERN.(Pname);


Pind=1:length(P);

PL = Pind(matches(P,'SPD'));
PR = Pind(matches(P,'CDU'));

if length(P)>6
    
    [reject,s,critVal,muNull,Orders] = H0RU(D,version,PL,PR);
    Est.reject=reject;
    Est.s=s;
    Est.critVal=critVal;
    Est.muNull=muNull;
    Est.Orders=Orders;
    Fname = strcat(Dname,'RUFv',int2str(version));
    save(Fname,'Est')
    
end
delete(gcp('nocreate'))
exit